import { create } from 'zustand'
import { devtools } from 'zustand/middleware'
import { getRouters } from '@/api/login'
import { constantRoutes, dynamicRoutes } from '@/route/index.jsx'

export const useRouteStore = create(devtools(set => ({
  routes: [],
  // 异步方法：获取用户信息
  filterRoutes: async () => {
    try {
      // console.log('constantRoutes', constantRoutes)
      // console.log('dynamicRoutes', dynamicRoutes)
      let routes = await getRouters()
      let filter = (list, routes) => {
        return list.filter(route => {
          const match = routes.find(r => r.path === route.path);
          if (match) {
            if (route.children && match.children) {
              route.children = filterAsyncRoutes(route.children, match.children);
            }
            return true;
          }
          return false;
        });
      };
      let filteRoutes = filter(dynamicRoutes, routes)
      console.log('filterRoutes', filteRoutes)
      set({ routes: [...constantRoutes, ...filteRoutes] })
    } catch (error) {
      console.error('获取路由失败', error)
      throw error
    }
  },
})))
